﻿/* ============================================================================
(c) Copyright 2012 Hewlett-Packard Development Company, L.P.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
============================================================================ */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using HPCloud.Objects.Domain.Compute.Networking;
using HPCloud.Objects.Domain.Compute.Security;
using System.Runtime.Serialization;
using HPCloud.Objects.Domain.Compute.Common;
using Newtonsoft.Json;

namespace HPCloud.Objects.Domain.Compute
{
    [System.Xml.Serialization.XmlRootAttribute(ElementName = "metadata", Namespace = "http://docs.openstack.org/compute/api/v1.1", IsNullable = false)]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://docs.openstack.org/compute/api/v1.1")]
    [System.Runtime.Serialization.DataContractAttribute(Name = "metadata")]
    public class MetaData : Dictionary<string, string>
    {
        public List<KeyValuePair<string, string>> ToKeypairs()
        {            
            return null;
            //List<KeyValuePair<string, string>> kvps = new List<KeyValuePair<string, string>>();
            //foreach (MetaDataElement element in this)
            //{
            //    KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(element.Key, element.Value);
            //    kvps.Add(kvp);
            //}
            //return kvps;
        }
    }

    #region JSON Support classes  
    // Type created for JSON at <<root>>
    [System.Runtime.Serialization.DataContractAttribute(Name = "server")]
    public partial class SingleServerClass : BaseEntity
    {
        [System.Runtime.Serialization.DataMemberAttribute()]
        public Server server;
    }

    // Type created for JSON at <<root>> for deserializing list
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class RootServerClass
    {
        [System.Runtime.Serialization.DataMemberAttribute()]
        public Server[] servers;
    }
    public class Addresses
    {
        [JsonProperty("private")]
        public IList<IPAddress> Private { get; set; }
    }
    public class IPAddress
    {

        [JsonProperty("version")]
        public int Version { get; set; }

        [JsonProperty("addr")]
        public string Addr { get; set; }
    }
   
    #endregion

    [System.Runtime.Serialization.DataContractAttribute(Name = "server")]
    public class Server : BaseEntity
    {        
        private string _status;
        private int _progress;
        private string _hostId;
        private EntityRef _flavor;
        private EntityRef _image;
        //private Private[] _addresses;

        [JsonProperty("addresses")]
        public Addresses Addresses { get; set; }

        private MetaData _metaData;
        private DateTime _lastUpdatedDate;
        private DateTime _creationDate;
        private Link[] _links;
        private string _uuid;
        //private List<SecurityGroup> _securityGroups;
        private List<EntityRef> _securityGroups;
        private string _keyName;
        private string _adminPassword;

       
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================  
        public Server()
        {}
        #region Properties
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================  
        [DataMember(Name = "adminPass")]
        public string AdminPassword
        {
            get { return _adminPassword; }
            set { _adminPassword = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================  
        [System.Xml.Serialization.XmlElementAttribute("link", Namespace = "http://www.w3.org/2005/Atom")]
        public Link[] Links
        {
            get
            {
                return _links;
            }
            set
            {
                _links = value;
            }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "metadata")]
        public MetaData MetaData
        {
            get { return _metaData; }
            set { _metaData = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//======================================================================== 
        [DataMember(Name = "security_groups")]
        public List<EntityRef> SecurityGroups
        {
            get { return _securityGroups; }
            set { _securityGroups = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "flavor")]
        public EntityRef Flavor
        {
            get { return _flavor; }
            set { _flavor = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "image")]
        public EntityRef Image
        {
            get { return _image; }
            set { _image = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "key_name")]
        public string KeyName
        {
            get { return _keyName; }
            set { _keyName = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "created")]
        public DateTime CreationDate
        {
            get { return _creationDate; }
            set { _creationDate = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "updated")]
        public DateTime LastUpdatedDate
        {
            get { return _lastUpdatedDate; }
            set { _lastUpdatedDate = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        public string DisplayIPAddresses
        {
            get
            {               
                string addressList = null;
                if (this.Addresses.Private == null)
                    return "No addresses found";
                foreach (IPAddress address in this.Addresses.Private) {
                    addressList = addressList + address.Addr + ", ";             
                }
                if (addressList != null)
                    return "private = " + addressList.TrimEnd().TrimEnd(',');
                else
                    return "";
            }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "uuid")]
        public string UUID
        {
            get { return _uuid; }
            set { _uuid = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "status")]
        public string Status
        {
            get { return _status; }
            set { _status = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "progress")]
        public int Progress
        {
            get { return _progress; }
            set { _progress = value; }
        }
//========================================================================
/// <summary>
/// 
/// </summary>
//========================================================================
        [DataMember(Name = "hostId")]
        public string HostId
        {
            get { return _hostId; }
            set { _hostId = value; }
        }      
        #endregion
    }
}
